using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Se.Framework.Web.Mvc.GenericCrud;
using Trust.Casas.Web.Areas.Admin.Controllers.CustomGrid;
using Trust.Casas.Web.Areas.Admin.Models;

namespace Trust.Casas.Web.Areas.Admin.Controllers
{
    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    public class EntityManipulatorController : GenericCrudController<GenericCrudViewModel>
    {
        static EntityManipulatorController()
        {
            GridBuilderFactory.Factory.RegisterBuilder(new GridBuilderDefault<UserMetadata>());
            GridBuilderFactory.Factory.RegisterBuilder(new GridBuilderDefault<UserDetailMetadata>());
            GridBuilderFactory.Factory.RegisterBuilder(new GridBuilderDefault<BuildingTypeMetadata>());
            GridBuilderFactory.Factory.RegisterBuilder(new RegionGrigBuilder());
        }

        public ActionResult Index()
        {
            return View(GridBuilderFactory.Factory.Builders);
        }

        public ActionResult Multiple(string entities)
        {
            List<GenericCrudViewModel> entityList = new List<GenericCrudViewModel>();
            foreach (var item in entities.Split(','))
            {
                var builder = GridBuilderFactory.Factory.FromType(Se.Framework.Common.Config.Providers.TypeProviderManager.Default.GetType(item));
                entityList.Add(new GenericCrudViewModel
                {
                    Grid = builder.Build(this.Url),
                    Title = builder.TableProperties.Name
                });
            }
            return View(entityList);
        }

        public JsonResult UploadFile(string qqfile, string entity)
        {
            //this.entity = entity;
            //var file = new CsvFile(this);
            //Exception error = null;
            //file.UpdateFromQqFileUploader(qqfile);
            //try
            //{
            //    if (file.Error == null)
            //    {
            //        var set = Context.Set(GetEntityType());
            //        foreach (var item in file.ParseContent())
            //        {
            //            set.Add(item);
            //        }
            //    }
            //    error = file.Error;
            //}
            //catch (Exception ex)
            //{
            //    error = ex;
            //}
            //Context.SaveChanges();
            return Json(new
            {
                success = true,
                error = string.Empty
            }, "text/html", JsonRequestBehavior.AllowGet);
        }

        public ActionResult ExportToExcel(string entity)
        {
            //this.entity = entity;
            //var model = BuildModel();
            //var set = Context.Set(GetEntityType());
            //string fileName = string.Format("{0}.xls", entity);
            //model.Grid.ExportToExcel(set, fileName);
            return View();
        }
    }
}